package org.ccay.demo.tree.service.impl;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.core.base.BatchVO;
import org.ccay.core.base.PageVO;
import org.ccay.core.base.PagedResult;
import org.ccay.core.exception.ApplicationException;
import org.ccay.core.ioc.ISelfInject;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;
import org.ccay.demo.tree.TreeVO;
import org.ccay.demo.tree.dao.ITreeDao;
import org.ccay.demo.tree.service.ITreeService;

/**
 * 测试--树service实现
 */
@Named
public class TreeService implements ITreeService, ISelfInject{

	private static final ILogger logger = CcayLoggerFactory.getLogger(TreeService.class);
	
	@Inject
	private ITreeDao treeDao;
	private ITreeService self;
	
	/**
	 * 通过条件查询节点信息	
	 * @return
	 */
	public List<TreeVO> findTreeListByCondition(TreeVO queryTree) {
		logger.debug("Before findAllTreeList");
		return treeDao.findTreeListByCondition(queryTree);
	}

	/**
	 * 通过条件查询分页节点信息
	 * @param queryTree
	 * @param pageVO
	 * @return
	 */
	public PagedResult<TreeVO> findPagedTreeList(TreeVO queryTree, PageVO pageVO) {
		logger.debug("Before findPagedTreeList");
		return treeDao.findPagedTreeList(queryTree, pageVO);
	}

	/**
	 * 批量增加节点信息
	 * @param newTrees
	 * @throws ApplicationException
	 */
	public void createTrees(List<TreeVO> newTrees) throws ApplicationException {
		treeDao.createTrees(newTrees);
		logger.debug("After createTrees");
	}

	/**
	 * 批量修改节点信息
	 * @param existedTrees
	 * @throws ApplicationException
	 */
	public void deleteTrees(List<TreeVO> existedTrees) throws ApplicationException {
		treeDao.deleteTrees(existedTrees);
		logger.debug("After deleteTrees");
	}

	/**
	 * 批量修改节点信息
	 * @param existedTrees
	 * @throws ApplicationException
	 */
	public void updateTrees(List<TreeVO> existedTrees) throws ApplicationException {
		treeDao.updateTrees(existedTrees);
		logger.debug("After updateTrees");
	}

	/**
	 * 批量操作节点信息
	 * @param batchItems
	 * @throws ApplicationException
	 */
	public void batchOperation(BatchVO<TreeVO> batchItems, Boolean isLazyLoad) throws ApplicationException {
		// 如果对象不为空 执行
		if(!batchItems.getItems2Create().isEmpty()){
			self.createTrees(batchItems.getItems2Create());
			logger.debug("After batchOperation createTrees");
		}
		// 如果对象不为空 执行
		if(!batchItems.getItems2Delete().isEmpty()){
			if (isLazyLoad) {
				self.deleteTreesLazyLoad(batchItems.getItems2Delete());
			} else {
				self.deleteTrees(batchItems.getItems2Delete());
			}
			logger.debug("After batchOperation deleteTrees");
		}
		// 如果对象不为空 执行
		if(!batchItems.getItems2Update().isEmpty()){
			self.updateTrees(batchItems.getItems2Update());
			logger.debug("After batchOperation updateTrees");
		}
	}

	public void setSelf(Object self) {
		this.self = (ITreeService)self;
	}

	public void createTree(TreeVO newTrees) throws ApplicationException {
		 treeDao.createTree(newTrees);
	}
	public void deleteTree(TreeVO existedTrees) throws ApplicationException {
		 treeDao.deleteTree(existedTrees);		
	}
	
	public void updateTree(TreeVO existedTrees) throws ApplicationException {
		 treeDao.updateTree(existedTrees);	
	}

	public List<TreeVO> findTreeListByParentId(TreeVO queryTree) {
		return treeDao.findTreeListByParentId(queryTree);
	}

	public void deleteTreesLazyLoad(List<TreeVO> existedTrees) throws ApplicationException {
		treeDao.deleteTreesLazyLoad(existedTrees);
	}
	
}
